package com.css.apps.base.common.provider.json;

import com.css.apps.base.common.BaseEnvironment;
import com.css.apps.base.common.provider.OrgQueryProvider;
import com.css.apps.base.org.model.SOrg;
import com.css.apps.base.org.model.SUserOrg;
import com.css.apps.base.post.model.SPost;
import com.css.apps.base.user.model.SUser;
import com.css.core.model.tree.Tree;
import com.css.core.model.tree.TreeCache;
import com.css.core.model.tree.TreeNode;
import com.css.db.query.QueryCache;
import com.css.util.StringHelper;
import com.css.webbase.WebBaseUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class OrgJsonProvider {

	public JSONArray getOrgCheckbox(List<String> orgIds) throws JSONException {
		return getOrgCheckbox(orgIds, true);
	}
	
	public JSONArray getOrgCheckbox(List<String> orgIds, boolean withChildren) {
		return getOrgCheckbox(orgIds, withChildren, null);
	}
	
	public JSONArray getOrgCheckbox(List<String> orgIds, boolean withChildren, List<String> chkDiabled) throws JSONException {
		List<TreeNode> lst = new ArrayList<TreeNode>();
		Tree tree = new TreeCache(SOrg.class);
		TreeNode rootNode = tree.getRoot().get(0);
		lst.add(rootNode);
		if (withChildren) {
			lst.addAll(rootNode.getAllChildren());
		} else {
			lst.addAll(rootNode.getChildren());
		}
		JSONObject one;
		JSONArray orgTree = new JSONArray();
		for (TreeNode o : lst) {
			String orgId = o.getNodeId();
			SOrg item = QueryCache.get(SOrg.class, orgId);
			if (item == null)
				continue;
			one = new JSONObject();
			one.put("id", orgId);
			one.put("name", item.getName());
			one.put("pId", item.getParentId());
			if ("root".equals(item.getParentId())) {
				one.put("open", true);
			}
			if (!withChildren || (o.getChildren() == null || o.getChildren().size() == 0)) {
				one.put("isParent", false);
			} else {
				one.put("isParent", true);
			}
			if (orgIds != null && orgIds.contains(orgId)) {
				one.put("checked", true);
			}
			if (chkDiabled != null && chkDiabled.contains(orgId)) {
				one.put("chkDisabled", true);
			}
			orgTree.add(one);
		}
		return orgTree;
	}
	
	public JSONArray getAllOrgUserTree(String orgId) throws JSONException {// 10
		List<TreeNode> lst = new ArrayList<TreeNode>();
		Tree tree = new TreeCache(SOrg.class);
		TreeNode rootNode = tree.getRoot().get(0);
		lst.add(rootNode);
		lst.addAll(rootNode.getAllChildren());
		JSONObject one;
		JSONArray orgTree = new JSONArray();
		for (TreeNode o : lst) {
			SOrg item = QueryCache.get(SOrg.class, o.getNodeId());
			if (item == null) {
				continue;
			}
			one = new JSONObject();
			one.put("id", item.getUuid());
			one.put("name", item.getName());
			one.put("pId", item.getParentId());
			one.put("isParent", true);
			one.put("type", "org");
			orgTree.add(one);
			// orgTree.addAll(getUserJsonByOrgIdList(item.getUuid()));
		}
		for (TreeNode o : lst) {
			orgTree.addAll(getUserJsonByOrgIdList(o.getNodeId(), orgId));
		}
		return orgTree;
	}
	
	@SuppressWarnings("unchecked")
	private JSONArray getUserJsonByOrgIdList(String orgId, String nowOrgId) { // 当前组织机构id
		JSONArray jaTree = new JSONArray();
		List<String> listt = new QueryCache("select a.uuid from SUserOrg  a where a.orgId=:orgId ").setParameter("orgId", orgId)
				.list();
		List<SUserOrg> list = QueryCache.idToObj(SUserOrg.class, listt);
		if (list != null) {
			for (SUserOrg uo : list) {
				SUser u = QueryCache.get(SUser.class, uo.getUserId());
				if (u != null && BaseEnvironment.USERTYPE_NORMAL.equals(u.getUserType())
						&& !uo.getOrgId().equals(nowOrgId)) {
					JSONObject one = new JSONObject();
					one.put("id", u.getUserId());
					one.put("name", u.getRealName());
					one.put("pId", orgId);
					one.put("type", "user");
					one.put("isParent", false);
					jaTree.add(one);
				}
			}
		}
		return jaTree;
	}
	
	public JSONArray getOrgUserTree() throws JSONException {
		return getOrgUserTree(false, null);
	}
	
	public JSONArray getOrgUserTree(boolean checkStyle, List<String> userIds) {
		return getOrgUserTree(checkStyle, userIds, null);
	}
	
	public JSONArray getOrgUserTree(boolean checkStyle, List<String> userIds, List<String> chkDisabled) {
		List<TreeNode> lst = new ArrayList<TreeNode>();
		JSONArray orgTree = new JSONArray();
		Tree tree = new TreeCache(SOrg.class);
		TreeNode rootNode = tree.getRoot().get(0);
		JSONObject root = new JSONObject();
		root.put("id", rootNode.getNodeId());
		root.put("name", rootNode.getName());
		root.put("pId", rootNode.getParentId());
		root.put("isParent", true);
		root.put("open", true);
		orgTree.add(root);
		lst.addAll(rootNode.getAllChildren());
		JSONObject one;
		for (TreeNode o : lst) {
			SOrg item = QueryCache.get(SOrg.class, o.getNodeId());
			if (item == null)
				continue;
			one = new JSONObject();
			one.put("id", item.getUuid());
			one.put("name", item.getName());
			one.put("pId", item.getParentId());
			one.put("isParent", true);
			orgTree.add(one);
			if (o.getChildren() == null || o.getChildren().size() == 0) {
				orgTree.addAll(getUserJsonByOrgIdList(item.getUuid(), checkStyle, userIds, chkDisabled));
			}
		}
		return orgTree;
	
	}
	
	@SuppressWarnings("unchecked")
	private JSONArray getUserJsonByOrgIdList(String orgId, boolean isCheckOrRadio, List<String> userIds, List<String> chkDisabled) {
		JSONArray jaTree = new JSONArray();
		List<String> listt = new QueryCache("select a.uuid from SUserOrg  a where a.deptId = :orgId ").setParameter("orgId", orgId)
				.listCache();
		List<SUserOrg> list = QueryCache.idToObj(SUserOrg.class, listt);
		if (list != null) {
			for (SUserOrg uo : list) {
				String userId = uo.getUserId();
				SUser u = QueryCache.get(SUser.class, userId);
				if (u != null) {
					JSONObject one = new JSONObject();
					one.put("id", u.getUserId());
					one.put("name", u.getRealName());
					one.put("pId", orgId);
					one.put("isParent", false);
					if (userIds != null && userIds.contains(userId)) {
						if (isCheckOrRadio) {
							one.put("checked", true);
						}
					}
					if (chkDisabled != null && chkDisabled.contains(userId)) {
						one.put("chkDisabled", true);
					}
					jaTree.add(one);
				}
			}
		}
		return jaTree;
	}
	
	@SuppressWarnings("unchecked")
	public JSONArray getSubOrgRadio(String parentId, String orgId) throws JSONException {
		List<String> uuids = OrgQueryProvider.getOrganIds(parentId);
		List<SOrg> lst = QueryCache.idToObj(SOrg.class, uuids);
		JSONArray orgTree = new JSONArray();
		JSONObject one;
		List<String> ids;
		if (lst != null)
			for (SOrg item : lst) {
				one = new JSONObject();
				one.put("id", item.getUuid());
				one.put("name", item.getName());
				one.put("pId", item.getParentId());
				ids = OrgQueryProvider.getOrganIds(item.getUuid());
				if (ids == null || ids.size() < 1)
					one.put("isParent", false);
				else
					one.put("isParent", true);
				if (orgId != null && orgId.equals(item.getUuid()))
					one.put("checked", true);
				orgTree.add(one);
			}
		return orgTree;
	}
	
	@SuppressWarnings("unchecked")
	public JSONArray getOrgPostTree(String orgId) throws JSONException {
		List<String> postids = new QueryCache("select a.postId from SOrgPost a where a.orgId=:orgId")
				.setParameter("orgId", orgId).list();
		JSONArray jaTree = new JSONArray();
		JSONObject one = new JSONObject();
		one.put("id", "0");
		one.put("name", "所有岗位");
		one.put("pId", "-1");
		one.put("isParent", true);
		one.put("use", false);
		jaTree.add(one);
		List<String> list = new QueryCache("select a.uuid from SPost a").list();
		List<SPost> lst = QueryCache.idToObj(SPost.class, list);
		if (lst != null)
			for (SPost item : lst) {
				one = new JSONObject();
				one.put("id", item.getUuid());
				one.put("name", item.getName());
				one.put("pId", "0");
				one.put("isParent", false);
				one.put("use", true);
				if (postids.contains(item.getUuid()))
					one.put("checked", true);
				jaTree.add(one);
			}
		return jaTree;
	}
	
	// 获得孩子节点，封装成jsonArray
	public JSONArray getSubOrgan(String parentId, String checkedId) throws JSONException {
		Tree tree = new TreeCache(SOrg.class);
		TreeNode tn = null;
		JSONArray orgTree = new JSONArray();
		JSONObject one;
		if (StringHelper.isEmpty(parentId)) {
			tn = tree.getRoot().get(0);
			SOrg parentItem = QueryCache.get(SOrg.class, tn.getNodeId());
			if (parentItem != null) {
				one = new JSONObject();
				one.put("id", parentItem.getUuid());
				one.put("name", parentItem.getName());
				one.put("pId", parentItem.getParentId());
				one.put("isParent", true);
				one.put("open", true);
				if (StringHelper.isEmpty(parentItem.getParentId()) && StringHelper.isEmpty(checkedId)) {
					one.put("checked", true);
				}
				orgTree.add(one);
			}
		} else {
			tn = tree.getTreeNode(parentId);
		}
		List<TreeNode> lst = tn.getAllChildren();
		for (TreeNode node : lst) {
			SOrg item = QueryCache.get(SOrg.class, node.getNodeId());
			if (item != null) {
				one = new JSONObject();
				one.put("id", node.getNodeId());
				one.put("name", item.getName());
				one.put("pId", item.getParentId());
				one.put("isParent", !node.isLeaf());
				if (item.getUuid().equals(checkedId) || item.getUuid().equals(parentId)) {
					one.put("checked", true);
				}
				orgTree.add(one);
			}
		}

		return orgTree;
	}

	/**
	 * 获取分管机构
	 * @param parentId
	 * @param checkedId
	 * @return
	 * @throws JSONException
	 */
	public JSONArray getBrachOrgan(String parentId, String checkedId) throws JSONException {
		Tree tree = new TreeCache(SOrg.class);
		TreeNode tn = null;
		JSONArray orgTree = new JSONArray();
		JSONObject one;
		if (StringHelper.isEmpty(parentId)) {
			tn = tree.getTreeNode(WebBaseUtil.getCurrentUser().getOrganId());
			SOrg parentItem = QueryCache.get(SOrg.class, tn.getNodeId());
			if (parentItem != null) {
				one = new JSONObject();
				one.put("id", parentItem.getUuid());
				one.put("name", parentItem.getName());
				one.put("pId", parentItem.getParentId());
				one.put("isParent", true);
				one.put("open", true);
				if (StringHelper.isEmpty(parentItem.getParentId()) && StringHelper.isEmpty(checkedId)) {
					one.put("checked", true);
				}
				orgTree.add(one);
			}
		} else {
			tn = tree.getTreeNode(parentId);
		}
		List<TreeNode> lst = tn.getAllChildren();
		for (TreeNode node : lst) {
			SOrg item = QueryCache.get(SOrg.class, node.getNodeId());
			if (item != null) {
				one = new JSONObject();
				one.put("id", node.getNodeId());
				one.put("name", item.getName());
				one.put("pId", item.getParentId());
				one.put("isParent", !node.isLeaf());
				if (item.getUuid().equals(checkedId) || item.getUuid().equals(parentId)) {
					one.put("checked", true);
				}
				orgTree.add(one);
			}
		}

		return orgTree;
	}

	public JSONArray getOrgRadio(String orgId) throws JSONException {
		List<TreeNode> lst = new ArrayList<TreeNode>();
		Tree tree = new TreeCache(SOrg.class);
		TreeNode rootNode = tree.getRoot().get(0);
		lst.add(rootNode);
		lst.addAll(rootNode.getAllChildren());
		JSONObject one;
		JSONArray orgTree = new JSONArray();
		for (TreeNode o : lst) {
			SOrg item = QueryCache.get(SOrg.class, o.getNodeId());
			if (item == null)
				continue;
			one = new JSONObject();
			one.put("id", item.getUuid());
			one.put("name", item.getName());
			one.put("pId", item.getParentId());
			if (StringHelper.isEmpty(item.getParentId())) {
				one.put("open", true);
			}
			if (o.getChildren() == null || o.getChildren().size() == 0)
				one.put("isParent", false);
			else
				one.put("isParent", true);
			if (orgId != null && orgId.equals(item.getUuid()))
				one.put("checked", true);
			orgTree.add(one);
		}
		return orgTree;
	}

}
